package xyz.migle.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Map;
import javax.sql.DataSource;


public class DBUtil {

    private static DataSource dataSource;
    private static ThreadLocal<Connection> connHolder = new ThreadLocal<>();
    private static DBUtil instance = new DBUtil();

    private DBUtil() {
    }
    //FIXME:同一个线程中连接多个数据库或用多个用户连接同一数据库时会有问题！,设置一个MAP
    public static Connection getConnection(String url,
        String user, String password ) throws SQLException {
      System.out.println("创建数据库连接");
//        Connection connection = connHolder.get();
//        if (connection == null || connection.isClosed()) {
//            connection = DriverManager.getConnection(url, user, password);
//            connHolder.set(connection);
//        }
//        return connection;
        return DriverManager.getConnection(url, user, password);
    }



    protected static void rethrow(SQLException cause, String sql, Object... params)
            throws SQLException {

        String causeMessage = cause.getMessage();
        if (causeMessage == null) {
            causeMessage = "";
        }
        StringBuffer msg = new StringBuffer(causeMessage);

        msg.append(" SQL: ");
        msg.append(sql);
        msg.append(" Parameters: ");

        if (params == null) {
            msg.append("[]");
        } else {
            msg.append(Arrays.deepToString(params));
        }

        SQLException e = new SQLException(msg.toString(), cause.getSQLState(),
                cause.getErrorCode());
        e.setNextException(cause);

        throw e;
    }

    public static void main(String[] args) {
        System.out.println();
    }
}
